﻿Public Class SubeDuzeltmeGenel : Inherits DuzeltmeGenel

	Public Overrides Sub GenericEvent(e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Shown
				Conn = Local
				Database = ""
		End Select
		MyBase.GenericEvent(e)
	End Sub

End Class

Public Class DuzeltmeGenel
	Inherits Process

	Public Conn As Token
	Public Database As String

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Text = "Genel Kayıt Düzeltme"
				InitialHelp = "Lütfen düzeltilecek kütüğü seçin."
				AddCombo("@Table", "Kütük adı", "", "nl;width=12", "", "Kütüğü seçmeniz gerekir.")
				StartBody()
				AddList()
				List.AddColumns("name(Alan adı;r;100),dot(;c;30),*value(Değer;l;100%)")
			Case EventType.Shown
				Source("Table") = Sistem.Tablolar(Conn, Database)
			Case EventType.ValidateForm
				Rs = Sistem.Alanlar(Conn, Database, Input("Table"))
				List.DataSource = Rs
				Help = "Sorgu kriterlerini girin."
				Buttons.Show("Next.Reset")
				e.Result = True
			Case EventType.Save
				Q("select count(*) from " & If(Database = "", "", Database & ".dbo.") & Input("Table") & " where 1=1")
				For Each Row As DataGridViewRow In List.Rows
					If Row.Cells(2).Value <> "" Then Q("and " & Row.Cells(0).Value & " = '" & Row.Cells(2).Value & "'")
				Next
				Rs = Conn.OpenSQL("*")
				If Rs(0) = 0 Then
					Warning("Kayıt bulunamadı. Kriterlerinizi gözden geçirin.")
				ElseIf Rs(0) > 250 Then
					Warning(Rs(0) & " kayıt bulundu. Kriterlerinizi gözden geçirin.")
				Else
					If Not Confirm(Rs(0) & " kayıt bulundu. Düzeltme işlemine devam etmek istiyor musunuz?") Then Exit Sub
					Q("select * from " & If(Database = "", "", Database & ".dbo.") & Input("Table") & " where 1=1")
					For Each Row As DataGridViewRow In List.Rows
						If Row.Cells(2).Value <> "" Then Q("and " & Row.Cells(0).Value & " = '" & Row.Cells(2).Value & "'")
					Next
					Rs = Conn.OpenSQL("*")
					Dim Prc As New DuzeltmeGenelKayit
					Prc.AddArgument("database", Database)
					Prc.AddArgument("conn", Conn)
					Prc.AddArgument("table", Input("Table"))
					Prc.AddArgument("rs", Rs)
					Window.Launch(Prc)
					'Process.Run(Window)
				End If
		End Select
	End Sub

End Class

Public Class DuzeltmeGenelKayit
	Inherits Process

	Dim ChangesTable As String
	Dim ActualTable As String

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Kayıt Düzeltme", "Kaydı düzelttikten sonra Kaydet tuşuna basıp kaydedin.")
				AddList()
			Case EventType.Shown
				ChangesTable = If(Arguments("database") = "", "", "ssd.dbo.") & "_changes"
				ActualTable = If(Arguments("database") = "", "", Arguments("database") & ".dbo.") & Arguments("table")
				For Each Column As DataColumn In Arguments("rs").Columns
					List.AddColumn("*" & Column.ColumnName, Column.ColumnName, "l", "120")
				Next
				List.RowHeadersVisible = True
				List.DataSource = Arguments("rs")
				For Each Col As DataGridViewColumn In List.Columns
					Col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
				Next
				Buttons.Show("Save")
			Case EventType.Other
				If e.Name = "List.Delete" Then
					If Not Confirm("Bu kaydı silmek istediğinizden emin misiniz?") Then Exit Sub
					Dim Row As DataGridViewRow = List.SelectedRows(0)
					Rs = Arguments("rs").Select("rowid=" & Row.Cells("rowid").Value)
					Dim AllChanges As String = ""
					AllChanges &= "insert into " & ChangesTable & "(tablo,rowid,alan,eski,yeni,islemtar,kaykul) values ('" & Arguments("table") & "'," & Rs("rowid") & ",'<deleted>','<deleted>','<deleted>','" & YMD(Today) & "','" & User.Kaykul & "')" & vbCrLf
					AllChanges &= "delete from " & ActualTable & " where rowid = " & Rs("rowid") & vbCrLf
					Try
						Sistem.GenelDuzelt(Arguments("conn"), AllChanges)
						List.Rows.Remove(Row)
						Info("Kayıt silinmiştir.")
					Catch ex As Exception
						Rollback(ex.Message)
					End Try
				End If
			Case EventType.Save
				Dim AllChanges As String = ""
				Dim ReadableChanges As String = ""
				For Each Row As DataGridViewRow In List.Rows
					Dim Changes As String = ""
					Rs = Arguments("rs").Select("rowid=" & Row.Cells("rowid").Value)
					For Each Col As DataColumn In Rs.Columns
						If (Not IsDBNull(Rs(Col.ColumnName)) AndAlso Rs(Col.ColumnName) <> Row.Cells(Col.ColumnName).Value) Or (IsDBNull(Rs(Col.ColumnName)) And Not IsDBNull(Row.Cells(Col.ColumnName).Value)) Then
							Changes &= "," & Col.ColumnName & " = '" & Replace(Row.Cells(Col.ColumnName).Value, "'", "''") & "'"
							ReadableChanges = "Kayıt No: " & Rs("rowid") & ", Alan: " & Col.ColumnName & ", " & Rs(Col.ColumnName) & " -> " & Row.Cells(Col.ColumnName).Value & vbCrLf
                            AllChanges &= "insert into " & ChangesTable & "(tablo,rowid,alan,eski,yeni,islemtar,kaykul) values ('" & Arguments("table") & "'," & Rs("rowid") & ",'" & Col.ColumnName & "','" & Replace(IsNull(Rs(Col.ColumnName), "<null>"), "'", "''") & "','" & Replace(Row.Cells(Col.ColumnName).Value, "'", "''") & "','" & YMD(Today) & "','" & User.Kaykul & "')" & vbCrLf
						End If
					Next
					If Changes <> "" Then AllChanges &= "update " & ActualTable & " set " & Mid(Changes, 2) & " where rowid = " & Rs("rowid") & vbCrLf
				Next
				If AllChanges = "" Then
					Warning("Hiçbir değişiklik yapmadınız.")
					Exit Sub
				End If
				If Not Confirm("Yaptığınız değişiklikler:" & vbCrLf & vbCrLf & ReadableChanges & vbCrLf & "Kaydetmek istiyor musunuz?") Then Exit Sub
				Try
					Sistem.GenelDuzelt(Arguments("conn"), AllChanges)
					Info("Değişiklikleriniz kaydedilmiştir.")
					Close()
				Catch ex As Exception
					Rollback(ex.Message)
				End Try
		End Select
	End Sub

End Class
